package com.hao.staservice.schedule;

import com.hao.staservice.service.StatisticsDailyService;
import com.hao.staservice.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author chenwenhao
 * @version 1.0.0
 * @ClassName ScheduledTask.java
 * @createTime 2021/1/27,21:16
 * @Description 定时任务
 */
@Component
public class ScheduledTask {

    @Autowired
    private StatisticsDailyService staService;

    /**
     * task1,测试类，每隔5s执行一次
     * cron表达式只能写6位，
     * 否则报错：Cron expression must consist of 6 fields
     */
    //@Scheduled(cron = "0/5 * * * * ?")
    public void task1() {
        System.out.println("执行了定时任务。。。");
    }

    /**
     * 每天1点，执行，统计前一天的数据
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void registerCountTask() {
        String date = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
        staService.registerCount(date);
    }

}
